package yu.com.time.calclator

import yu.com.time.unit.DateUnit
import yu.com.time.unit.DateUnit.*
import java.time.LocalDate
import java.time.temporal.ChronoUnit


/**
 * jdk8日期计算器
 * created zpf on 2018/10/23 0023
 * @param origTime 原始时间
 * @param unit     时间计算单位
 */
open class LocalDateCalculator(val origTime: LocalDate, val unit: DateUnit) : Calculator<LocalDate, LocalDate> {

    override fun plus(addition: Long): LocalDate {
        return when (unit) {
            YEAR -> origTime.plusYears(addition)
            MONTH -> origTime.plusMonths(addition)
            WEEK -> origTime.plusWeeks(addition)
            DAY -> origTime.plusDays(addition)
        }
    }

    override fun minus(subtraction: LocalDate): Long {
        return when (unit) {
            YEAR -> subtraction.until(origTime, ChronoUnit.YEARS)
            MONTH -> subtraction.until(origTime, ChronoUnit.MONTHS)
            WEEK -> subtraction.until(origTime, ChronoUnit.WEEKS)
            DAY -> subtraction.until(origTime, ChronoUnit.DAYS)
        }
    }
}